package com.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;

/**
 *
 * zookeeper只支持字节数组，序列化交由开发人员完成
 *
 * Created by dong peng on 2017/12/6.
 */
public class ZookeeperConnectionSample {

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        ZooKeeper zooKeeper = new ZooKeeper("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183",5000,new MyWatcher());
        System.out.println(zooKeeper.toString());

        // 设置ACL权限
//        zooKeeper.addAuthInfo("digest","foo:true".getBytes());

        // 创建
        String path = zooKeeper.create("/test","你好".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(path);

        // 删除 -1表示删除任意版本
//        zooKeeper.delete("/test",-1);

        // 查询目录树
        List<String> children = zooKeeper.getChildren("/test",true);
        System.out.println(children);

        // 查询节点上的数据
        Stat stat = new Stat();
        byte[] datas =  zooKeeper.getData("/test",null,stat);
        System.out.println("获取的数据："+new String(datas));
        System.out.println(stat.toString());

        // 判断节点是否存在
        Stat stat1 = zooKeeper.exists("/test",true);
        System.out.println(stat1);

        //获取状态
        System.out.println("==============获取状态state===============");
        ZooKeeper.States stat2 = zooKeeper.getState();
        System.out.println(stat2);

    }
}

